// pages/index/list/list.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postsList: [], //总的活动
    postsShowSwiperList: [], //轮播图显示的活动
    currentPage: 0, //要跳过查询的页数
    limitPage: 3,//首先显示3条数据（之后加载时都增加3条数据，直到再次加载不够3条）
    isEmpty: false, //当前查询出来的数据是否为空
    totalCount: 0, //总活动数量
    endPage: 0, //最后一页加载多少条
    totalPage: 0, //总页数
    curIndex: 0,
    windowHeight1: 0,
    windowWidth1: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    var molist = new Array();
    var encodeUrl = encodeURI(app.globalData.hostName + '/microappointment/activity/search?keyWord=' + options.keyWord + '&pageNum=1&pageSize=200');
    console.log("search res", encodeUrl)
    
    wx.request({
      url: encodeUrl,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var results = res.data.data;
        for (var i = 0; i < results.length; i++) {
          var unixCreateTimestamp = new Date(results[i].createTime).toDateString();
          var unixExpireTimestamp = new Date(results[i].expireTime).toDateString();
          var tempPicArray = new Array();
          if (results[i].pictureList.length > 0) {
            tempPicArray = results[i].pictureList;
          } else {
            tempPicArray = [{ pictureUrl: "https://www.daliandaxue.cn/img/not_upload.jpg" }];
          }
          var siglePic = tempPicArray[0];
          var jsonA = {
            "publisherPic": results[i].publisherPic || "/image/1.jpg",
            "publisherName": results[i].publisherName || '',
            "createTime": unixCreateTimestamp,
            "title": results[i].title || '',
            "actPic": siglePic.pictureUrl || '',
            "endtime": unixExpireTimestamp || '',
            "id": results[i].id || '',
            "publisherId": results[i].id || '',
            "address": results[i].location || ''
          }
          molist.push(jsonA);
          console.log("molist", molist);
        }
        self.setData({
          postsShowSwiperList: molist
        })
        self.onSetData(molist, self.data.currentPage);
        //self.fetchPostsData(self.data); //加载首页信息
      },
      error: function (error) {
        console.log(error)
      }
    })
  },
  //数据存储
  onSetData: function (data) {
    console.log(data.length);
    let page = this.data.currentPage + 1;
    //设置数据
    data = data || [];
    this.setData({
      postsList: page === 1 || page === undefined ? data : this.data.postsList.concat(data),
    });
    console.log(this.data.postsList, page);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  clickonShareAppMessage: function () {

  },
  goToDetail: function (e) {
    wx.navigateTo({
      //url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      url: "/pages/detail/detail?id=" + e.currentTarget.dataset.id
    })
  }
})